/*

    mui.init();
    mui.ready(function() {
        /!**
         * 获取对象属性的值
         * 主要用于过滤三级联动中，可能出现的最低级的数据不存在的情况，实际开发中需要注意这一点；
         * @param {Object} obj 对象
         * @param {String} param 属性名
         *!/
        //级联示例
        //获取到当前的日时分
        $("#date").click(function(e){
            e.preventDefault();
            var dtPicker = new mui.DtPicker({
                type: 'day',
            });
            dtPicker.show(function (selectItems) {
                console.log(selectItems)
                $("#date-result").html(selectItems.d.text+' '+selectItems.h.text+':'+selectItems.i.text);
                dtPicker.dispose();  //释放组件资源，释放后将将不能再操作组件.所以每次用完便立即调用 dispose 进行释放，下次用时再创建新实例
            })

        });


        //普通示例

        var userPicker = new mui.PopPicker({
            layer: 3,
            pickers: [ {
                value:'zz',
                text:'1',

            },
                {
                    value:'zz',
                    text:'2',

                }]

        });
 
        var showUserPickerButton = document.getElementById('vehicles-num');
        var userResult = document.getElementById('vehicles-num-result');
        showUserPickerButton.addEventListener('tap', function(event) {
            userPicker.show(function(items) {
                userResult.innerText = JSON.stringify(items[0]);
                //返回 false 可以阻止选择框的关闭
                //return false;
            });
        }, false);
    });
*/
$(function () {
    //TODO：从服务器获取
    var startTime = '06:59';//所选路线开始时间,时间要减1，比如这是07：00开始
    var endTime = '19:00';//所选路线结束时间

    var now = new Date(),
        minDate = now,
        temp_now = now,
        maxDate = new Date(now.getFullYear(), now.getMonth(), now.getDate()+2, 23, 59);

    if(now.getHours()<startTime.substr(0,2)){
        temp_now.setHours(startTime.substr(0,2));
        temp_now.setMinutes(0)
    }else if(now.getHours()>endTime.substr(0,2)){
        temp_now.setHours(startTime.substr(0,2));
        temp_now.setDate(now.getDate()+1);
        temp_now.setMinutes(0)
    }else{
        var m = now.getMinutes()%60;

        if(m < 30){
            temp_now.setMinutes(30)
        }else{
            temp_now.setHours(temp_now.getHours()+1);
            temp_now.setMinutes(0)
        }
    }

    $('#date').mobiscroll().datetime({
        theme: 'ios',
        lang: 'zh',
        placeholder: '请选择...',//空白等待提示  
        display: 'bottom',
        controls: ['date', 'time', 'calendar'],
        headerText: '选择动车到达或出发时间',
        defaultValue: minDate,
        min: minDate,
        max: maxDate,
        cssClass: 'user-date',
        height: 60,
        dateWheels: '|d|',
        daySuffix: '日',
        nowText: "2",
        timeWheels: 'HHii',
        hourText: '时',
        rows:5,
        circular:false,
        invalid:[
            { start: '00:00', end: startTime }, // Every day
            { start: endTime, end: '23:59' } // Every day
        ],
        onChange: function (event, inst) {
        },
        formatValue: function (data) {
            var date = new Date().getDate();
            var d = new Date();
            var day = '';
            if (data[0]==date) { day = '今天' } else if(data[0]==date+1) {day='明天'} else if(data[0] == date+2) {day = '后天'} else {day = data[0]+'日'}
            var fullval = d.getFullYear()+"-"+(d.getMonth()+1)+"-" + pad(data[0],2)+' '+ pad(data[1],2)+':'+ pad(data[2],2);
            $('#date').attr('relval',fullval);
            return day+' '+pad(data[1],2)+ ':' + pad(data[2],2);
        },
        onShow: function (event, inst) {
            now = new Date();
            $('.user-date .mbsc-dt-whl-d .mbsc-sc-itm[data-val='+now.getDate()+']').html('今天');;
            $('.user-date .mbsc-dt-whl-d .mbsc-sc-itm[data-val='+(now.getDate()+1)+']').html('明天');
            $('.user-date .mbsc-dt-whl-d .mbsc-sc-itm[data-val='+(now.getDate()+2)+']').html('后天');
            $("#date").css({'color':'#6B7886'});
            $("#vehicles-num-result").css({'color':'#6B7886'});
        },
        onBeforeShow:function(){
            minDate = new Date().getDate();
        },
        onCancel:function(){
            $("#date").css({'color':'#aaabb1'});
            $("#vehicles-num-result").css({'color':'#aaabb1'});
        }
        /*onMonthLoading: function (event, inst) {
            var year = event.year,
                month = event.month;
            inst.settings.marked = [];
                inst.settings.marked.push({
                    d: new Date(1), // Make sure that a javascript date object is passed
                    text: 1
                });
                // If no text needed:
                // inst.settings.marked.push(new Date(data[i].date));
            // Refresh the view to display new data
            inst.redraw();
        }*/
    });

    function pad(num, n) {  
    	var len = num.toString().length;  
        while(len < n) {  
            num = "0" + num;  
            len++;  
        }  
        return num; 
	}
    
    
    var numArr1=[0,1,2,3,4,5];
    var numArr2=[0,1,2,3,4,5];
    var numArr3=[0,1,2,3,4,5];
    
    var mobileSelect3 = new MobileSelect({
        trigger: '#vehicles-num-result',
        title: '选择用车人数',
        wheels: [
            {data: numArr1},
            {data: numArr2},
            {data: numArr3}
        ],
        position:[1, 0, 0],
        transitionEnd:function(indexArr, data){
            // console.log(data);
        },
        callback:function(indexArr, data){
        	if($('#pathid').attr('value')==''){
        		mui.alert('请选择路线'); 
        		$('.route_form').show();
        		$('#wapper').css('background','#f9fbfc');
        		$('.placeorder_form').hide();
        		$('#vehicles-num-result').html('请选择');
        		return;
        	}
            var format = '';
            var a = data[0] != 0 ?'成人'+ data[0] +'人 ' : '';
            var b = data[1] != 0 ?'儿童'+ data[1] +'人 ' : '';
            var c = data[2] != 0 ?'幼儿'+ data[2] +'人' : '';
            if (a == '' && b == '' && c == '') {
                format = '请选择人数';
            } else {
                format = a+b+c;
            }
            $('#vehicles-num-result').html(format);
            
            $.ajax({
    			type : 'post',
    			url : '/index.php/index/index/getprice',
    			data : {
    				id : $('#pathid').attr('value'),
    				cr : data[0],
    				et : data[1],
    				ye : data[2],
    			},
    			dataType : 'json',
    			success:function(data) {
    				$('#pricesum i').text(data.msg);
    				$('#pricesum').show();
    			}
    		});
        }
    });
});